package com.xxx.store.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.store.entity.Notice;
import com.xxx.store.entity.Type;
import com.xxx.store.entity.Vote;
import com.xxx.store.service.TypeService;
import com.xxx.store.service.VoteService;
import com.xxx.store.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-04-26
 */
@RestController
@RequestMapping("/type")//子类继承父类，可以使用父类中的方法
public class TypeController extends BaseController<Type, TypeService>{

    @Autowired
    TypeService typeService;

    @Autowired
    VoteService voteService;

    @Override
    public Result del(Integer id) {
        int count = voteService.count(new QueryWrapper<Vote>().eq("type_id", id));
        if (count > 0) {
            return Result.error("该类别下还有投票，不能删除！");
        }else{
            typeService.removeById(id);
            return Result.ok("删除成功!");
        }

    }
    @PostMapping("/add")
    public Result add(@RequestBody Type type){
        Date date = new Date();
        type.setCreateTime(date);
        typeService.save(type);
        return Result.ok("添加成功", type);
    }
}

